package com.dianrui.fmi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dianrui.fmi.R;
import com.dianrui.fmi.interfaces.NineAdapter2OperationCallBack;
import com.dianrui.fmi.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by leilei on 2017/9/1.
 */

public class NineAdapter extends BaseAdapter {
    List<String> list;
    NineAdapter2OperationCallBack nineAdapter2OperationCallBack;

    public NineAdapter(List<String> list,  NineAdapter2OperationCallBack nineAdapter2OperationCallBack) {
        this.list = list;
        this.nineAdapter2OperationCallBack=nineAdapter2OperationCallBack;

    }


    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nine_view, null);

            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (list != null && list.size()>0) {
            WindowManager wm = (WindowManager) parent.getContext().getSystemService(Context.WINDOW_SERVICE);
            holder.picItemNineView.setLayoutParams(new LinearLayout.LayoutParams(wm.getDefaultDisplay().getWidth()/3,wm.getDefaultDisplay().getWidth()/3));
            GlideUtil.loadImagesAsepct(list.get(position),holder.picItemNineView,wm.getDefaultDisplay().getWidth()/3, wm.getDefaultDisplay().getWidth()/3,parent.getContext());

            if (nineAdapter2OperationCallBack != null) {
                holder.picItemNineView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        nineAdapter2OperationCallBack.onClickImage(position, list.get(position), list);
                    }
                });

            }
        }




        return convertView;
    }

     class ViewHolder {
        @BindView(R.id.pic_item_nine_view)
        ImageView picItemNineView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}


